<?php

/**
 * topic actions.
 *
 * @package    s-test
 * @subpackage topic
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class topicActions extends sfActions
{
  public function executeShow(sfWebRequest $request){
	$this->forward404Unless($this->ktd_topic = Doctrine::getTable('KTDTopic')->find(array('id' => $request->getParameter('id'))), sprintf("Le sujet que vous souhaitez consulter n'existe pas ou plus."));
        if($this->ktd_topic->isNew())
                $this->ktd_topic->setOld();
        $this->pager = $this->ktd_topic->getPagerMessages(sfConfig::get("app_max_msg_on_topic"), $request->getParameter('page', 1));
        $this->ktd_messages = $this->pager->getResults();
        if($this->getUser()->hasPermission("post_message"))
                $this->form = new KTDMessageForm();

  }
  public function executeNew(sfWebRequest $request)
  {
    $this->forward404Unless($this->getUser()->getGuardUser()->hasPermission('create_topic'), "Vous n'avez pas la permission de créer de nouveau sujet");
    $this->ktd_forum = $this->getRoute()->getObject();
    $this->form = new KTDTopicForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $this->forward404Unless($this->getUser()->getGuardUser()->hasPermission('create_topic'), "Vous n'avez pas la permission de créer de nouveau sujet");
    $this->forward404Unless($this->ktd_forum = Doctrine::getTable('KTDForum')->find(array('id' => $request->getParameter('forum_id'))), sprintf("Le forum où vous souhaitez répondre n'existe pas ou plus."));
    
    $ktd_topic = new KTDTopic();
    $ktd_topic->setForumId($this->ktd_forum->getId());

    //On crée le topic
    $this->form = new KTDTopicForm($ktd_topic);
    $this->form->setOption('player_id', $this->getUser()->getGuardUser()->getId());
    $ktd_topic = $this->processForm($request, $this->form, $this->ktd_forum);

    $this->setTemplate('new');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $this->forward404Unless($ktd_topic = Doctrine::getTable('KTDTopic')->find(array($request->getParameter('id'))), sprintf('Object ktd_topic does not exist (%s).', $request->getParameter('id')));
    $this->forward404Unless($this->getUser()->getGuardUser()->hasPermission('delete_topic_all'), "Vous n'avez pas la permission de supprimer ce sujet");

    $ktd_forum = $ktd_topic->getKTDForum();
    KTDEventManager::getSingleton()->handleTopic(KTDTopicHandler::$onDelete, array("topic"=>$ktd_topic));
    $ktd_topic->delete();

    $this->redirect('show_forum', $ktd_forum);
  }

  protected function processForm(sfWebRequest $request, sfForm $form, KTDForum $ktd_forum)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
        $ktd_topic = $form->save();
        KTDEventManager::getSingleton()->handleTopic(KTDTopicHandler::$onCreate, array("player" => $this->getUser()->getGuardUser()));
        $this->redirect('show_forum', $ktd_forum);
    }
  }
}
